import { i18n } from '@/i18n';

export function getYesterdayStartAndEndTime() {
  let day1 = new Date();
  day1.setTime(day1.getTime()-24*60*60*1000);
  let year = day1.getFullYear(); //得到年份
  let month = day1.getMonth();//得到月份
  month = month + 1;
  let date = day1.getDate();//得到日期
  let monthStr =''
  let dateStr =''
  if (month < 10){
    monthStr = "0" + month;
  }else{
    monthStr = month.toString()
  }
  if (date < 10){
    dateStr = "0" + date;
  }else{
    dateStr = "0" + date;
  }
  let datetimeStart = year + "-" + month + "-" + date + " 00:00:00";
  let datetimeEnd = year + "-" + month + "-" + date + " 23:59:59";
  let date_s = new Date(datetimeStart);
  let date_e = new Date(datetimeEnd);
  let timestampStart = (date_s.getTime()) / 1000
  let timestampEnd = (date_e.getTime()) / 1000
  return {
    year,
    month,
    monthStr,
    date,
    dateStr,
    datetimeStart,
    datetimeEnd,
    timestampStart,
    timestampEnd
  }
}

export function getTodayStartAndEndTime() {
  let day1 = new Date();
  day1.setTime(day1.getTime());
  let year = day1.getFullYear(); //得到年份
  let month = day1.getMonth();//得到月份
  month = month + 1;
  let date = day1.getDate();//得到日期
  let monthStr =''
  let dateStr =''
  if (month < 10){
    monthStr = "0" + month;
  }else{
    monthStr = month.toString()
  }
  if (date < 10){
    dateStr = "0" + date;
  }else{
    dateStr = "0" + date;
  }
  let datetimeStart = year + "-" + month + "-" + date + " 00:00:00";
  let datetimeEnd = year + "-" + month + "-" + date + " 23:59:59";
  let date_s = new Date(datetimeStart);
  let date_e = new Date(datetimeEnd);
  let timestampStart = (date_s.getTime()) / 1000
  let timestampEnd = (date_e.getTime()) / 1000
  return {
    year,
    month,
    monthStr,
    date,
    dateStr,
    datetimeStart,
    datetimeEnd,
    timestampStart,
    timestampEnd
  }
}

export function getTomorrowStartAndEndTime() {
  let day1 = new Date();
  day1.setTime(day1.getTime()+24*60*60*1000);
  let year = day1.getFullYear(); //得到年份
  let month = day1.getMonth();//得到月份
  month = month + 1;
  let date = day1.getDate();//得到日期
  let monthStr =''
  let dateStr =''
  if (month < 10){
    monthStr = "0" + month;
  }else{
    monthStr = month.toString()
  }
  if (date < 10){
    dateStr = "0" + date;
  }else{
    dateStr = "0" + date;
  }
  let datetimeStart = year + "-" + month + "-" + date + " 00:00:00";
  let datetimeEnd = year + "-" + month + "-" + date + " 23:59:59";
  let date_s = new Date(datetimeStart);
  let date_e = new Date(datetimeEnd);
  let timestampStart = (date_s.getTime()) / 1000
  let timestampEnd = (date_e.getTime()) / 1000
  return {
    year,
    month,
    monthStr,
    date,
    dateStr,
    datetimeStart,
    datetimeEnd,
    timestampStart,
    timestampEnd
  }
}

//时间戳转年月日时分秒
export function getYMDHIS (timestamp: number) {
  let time = new Date(timestamp*1000)
  let year = time.getFullYear().toString()
  let month = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1).toString() : (time.getMonth() + 1).toString()
  let date = time.getDate() < 10 ? '0' + time.getDate().toString() : time.getDate().toString()
  let hours = time.getHours() < 10 ? '0'+time.getHours().toString() : time.getHours().toString()
  let minute =  time.getMinutes() < 10 ? '0'+time.getMinutes().toString() : time.getMinutes().toString()
  let second =  time.getSeconds() < 10 ? '0'+time.getSeconds().toString() : time.getSeconds().toString()

  return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
}
export function getYMDHIS2 (timestamp: number) {
  let time = new Date(timestamp*1000)
  let year = time.getFullYear().toString()
  let month = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1).toString() : (time.getMonth() + 1).toString()
  let date = time.getDate() < 10 ? '0' + time.getDate().toString() : time.getDate().toString()
  let hours = time.getHours() < 10 ? '0'+time.getHours().toString() : time.getHours().toString()
  let minute =  time.getMinutes() < 10 ? '0'+time.getMinutes().toString() : time.getMinutes().toString()
  let second =  time.getSeconds() < 10 ? '0'+time.getSeconds().toString() : time.getSeconds().toString()

  return year + month +  date +  hours +  minute + second
}

export function formatDateObj(time:Date){
  let year = time.getFullYear()
  let month = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1).toString() : (time.getMonth() + 1).toString()
  let date = time.getDate() < 10 ? '0' + time.getDate().toString() : time.getDate().toString()
  let hours = time.getHours() < 10 ? '0'+time.getHours().toString() : time.getHours().toString()
  let minute =  time.getMinutes() < 10 ? '0'+time.getMinutes().toString() : time.getMinutes().toString()
  let second =  time.getSeconds() < 10 ? '0'+time.getSeconds().toString() : time.getSeconds().toString()
  return {
    ymdhis: year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
  }
}

export function getYMDHISObjRes (timestamp: number) {
  let time = new Date(timestamp*1000)
  let year = time.getFullYear()
  let month = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1).toString() : (time.getMonth() + 1).toString()
  let date = time.getDate() < 10 ? '0' + time.getDate().toString() : time.getDate().toString()
  let hours = time.getHours() < 10 ? '0'+time.getHours().toString() : time.getHours().toString()
  let minute =  time.getMinutes() < 10 ? '0'+time.getMinutes().toString() : time.getMinutes().toString()
  let second =  time.getSeconds() < 10 ? '0'+time.getSeconds().toString() : time.getSeconds().toString()

  return {
    year , month ,date ,hours ,minute ,second
  }
}

export function getCurrentYMDHIS(){
  var date = new Date();
  const year = date.getFullYear();

  let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString()

  const day = date.getDate();
  const week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
  const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  const minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  const second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  return {
    ymdhis:year.toString()+month.toString()+day.toString()+hour.toString()+minute.toString()+second.toString()
  }
}

export function getCurrentSec(){
  return parseInt((new Date().getTime() / 1000).toString())
}



export function getTimeRangeShortCut(){
  const i18nObj = i18n.global


  const shortcuts = [
    {
      text: i18nObj.t("time.last") + "1" + i18nObj.t("time.min"),
      value: () => {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 60 * 1000)
        return [start, end]
      },
    },
    {
      text: i18nObj.t("time.last") + "10" + i18nObj.t("time.min"),
      value: () => {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 600 * 1000)
        return [start, end]
      },
    },
    {
      text: i18nObj.t("time.last") + "1" + i18nObj.t("time.hour"),
      value: () => {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000)
        return [start, end]
      },
    },
    {
      text: i18nObj.t("time.last") + "1" + i18nObj.t("time.day"),
      value: () => {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 *24)
        return [start, end]
      },
    },
    {
      text: i18nObj.t("time.last") + "7" + i18nObj.t("time.day"),
      value: () => {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
        return [start, end]
      },
    },
    {
      text: i18nObj.t("time.last") + "30" + i18nObj.t("time.day"),
      value: () => {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
        return [start, end]
      },
    },
    {
      text: i18nObj.t("time.last") + "90" + i18nObj.t("time.day"),
      value: () => {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
        return [start, end]
      },
    },
    {
      text: i18nObj.t("time.today"),
      value: () => {
        let today = new Date();
        today.setHours(0, 0, 0, 0); // 设置为当天的 00:00:00

        let tomorrow = new Date(today);
        tomorrow.setDate(today.getDate() + 1);
        tomorrow.setMilliseconds(tomorrow.getMilliseconds() - 1); // 设置为当天的 23:59:59
        return [today, tomorrow]
      },
    },
    {
      text: i18nObj.t("time.yesterday"),
      value: () => {
        let yesterday = new Date();
        yesterday.setDate(yesterday.getDate() - 1); // 获取昨天的日期

        yesterday.setHours(0, 0, 0, 0); // 设置为昨天的 00:00:00

        let today = new Date(yesterday);
        today.setDate(yesterday.getDate() + 1); // 获取今天的日期
        today.setMilliseconds(today.getMilliseconds() - 1); // 设置为昨天的 23:59:59

        return [yesterday, today]
      },
    },
    {
      text: i18nObj.t("time.current") + "" + i18nObj.t("time.month"),
      value: () => {
        const now = new Date(); // 获取当前日期
        const year = now.getFullYear(); // 当前年份
        const month = now.getMonth(); // 当前月份（0-11）

        // 获取本月的第一天
        const startOfMonth = new Date(year, month, 1);

        // 获取本月的最后一天
        // 注意：JavaScript 中月份是从 0 开始的，所以新的 Date 对象中月份设置为 month + 1，日期设置为 0，表示上个月的最后一天
        const endOfMonth = new Date(year, month + 1, 0);

        // 设置结束日期的时分秒为 23:59:59，以确保获取整个月的最后一刻
        endOfMonth.setHours(23, 59, 59, 999);

        return [startOfMonth, endOfMonth]
      },
    },
    {
      text: i18nObj.t("time.last") + "1" + i18nObj.t("time.month"),
      value: () => {
        //上个月
        const now = new Date(); // 获取当前日期
        const year = now.getFullYear(); // 当前年份
        const month = now.getMonth(); // 当前月份（0-11）

        // 获取上个月的第一天
        // 当月份为0（即1月）时，年份减1，月份设置为12月（JavaScript中为11）
        const startOfLastMonth = new Date(year, month - 1, 1);

        // 获取上个月的最后一天
        // 通过设置当前月的第0天，自动回到上个月的最后一天
        const endOfLastMonth = new Date(year, month, 0);

        // 设置结束日期的时分秒为 23:59:59，以确保获取整个月的最后一刻
        endOfLastMonth.setHours(23, 59, 59, 999);
        return [startOfLastMonth, endOfLastMonth]
      },
    },
  ]
  return shortcuts
}

export function getDateTimePickShortCut(){
  const i18nObj = i18n.global
  const shortcuts = [
    {
      text: i18nObj.t("time.today"),
      value:  () => {
        const date = new Date()
        date.setHours(0, 0, 0, 0);
        return date
      },
    },
    {
      text: i18nObj.t("time.yesterday"),
      value: () => {
        const date = new Date()
        date.setHours(0, 0, 0, 0);
        date.setTime(date.getTime() - 3600 * 1000 * 24)
        return date
      },
    },
    {
      text: i18nObj.t("time.tomorrow"),
      value: () => {
        const date = new Date()
        date.setHours(0, 0, 0, 0);
        date.setTime(date.getTime() + 3600 * 1000 * 24 )
        return date
      },
    },
  ]
  return shortcuts
}

export function getDataPageDefaultTimeRange(){
  //上个月
  const now = new Date(); // 获取当前日期
  const year = now.getFullYear(); // 当前年份
  const month = now.getMonth(); // 当前月份（0-11）

  // 获取上个月的第一天
  // 当月份为0（即1月）时，年份减1，月份设置为12月（JavaScript中为11）
  const startOfLastMonth = new Date(year, month - 1, 1);

  // 获取上个月的最后一天
  // 通过设置当前月的第0天，自动回到上个月的最后一天
  const endOfLastMonth = new Date(year, month, 0);

  // 设置结束日期的时分秒为 23:59:59，以确保获取整个月的最后一刻
  endOfLastMonth.setHours(23, 59, 59, 999);

  // 获取本月的第一天
  const startOfMonth = new Date(year, month, 1);

  // 获取本月的最后一天
  // 注意：JavaScript 中月份是从 0 开始的，所以新的 Date 对象中月份设置为 month + 1，日期设置为 0，表示上个月的最后一天
  const endOfMonth = new Date(year, month + 1, 0);

  // 设置结束日期的时分秒为 23:59:59，以确保获取整个月的最后一刻
  endOfMonth.setHours(23, 59, 59, 999);

  return [startOfMonth, endOfMonth]
}


export function parseNanosecondTimestamp(secnsStr:any) {
  secnsStr = secnsStr.toString()
  let seconds = Number(secnsStr.slice(0, -9)); // 从第一位开始截取到倒数第10位
  let nanoseconds = Number(secnsStr.slice(-9)); // 从倒数第9位开始截取到末尾
  // 计算毫秒和微秒
  const milliseconds = Math.floor(nanoseconds / 1000000); // 毫秒部分
  const microseconds = Math.floor(nanoseconds / 1000); // 微秒部分
  // 将毫秒和微秒补齐到3位数
  const millisecondsPadded = milliseconds.toString().padStart(3, '0');
  const microsecondsPadded = microseconds.toString().padStart(6, '0');
  // 返回结果
  return {
    seconds: seconds,
    milliseconds: millisecondsPadded,
    microseconds: microsecondsPadded,
    nanoseconds: nanoseconds.toString().padStart(9, '0') // 完整的纳秒（不足九位自动补0）
  };
}

export function formatYmdhismmm(time:number){
  const input = time.toString()
  if (input.length !== 17) {
    return ""
  }
  // 提取各个部分
  const year = input.substring(0, 4);
  const month = input.substring(4, 6);
  const day = input.substring(6, 8);
  const hours = input.substring(8, 10);
  const minutes = input.substring(10, 12);
  const seconds = input.substring(12, 14);
  const milliseconds = input.substring(14, 17);
  // 格式化为 yyyy-MM-dd HH:mm:ss.mmm
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`;
}

export function formatMillSecTimestamp(time:number){
  time = parseInt(time.toString())
  const date = new Date(time);  // 创建日期对象
  const year = date.getFullYear();  // 获取年份
  const month = String(date.getMonth() + 1).padStart(2, '0');  // 获取月份，+1 因为月份从 0 开始，padStart 保证两位数
  const day = String(date.getDate()).padStart(2, '0');  // 获取日期
  const hours = String(date.getHours()).padStart(2, '0');  // 获取小时
  const minutes = String(date.getMinutes()).padStart(2, '0');  // 获取分钟
  const seconds = String(date.getSeconds()).padStart(2, '0');  // 获取秒
  const milliseconds = String(date.getMilliseconds()).padStart(3, '0');  // 获取毫秒，padStart 保证 3 位数
  // 按照格式拼接字符串
  return {
    a:`${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`,
    b: Number(`${year}${month}${day}${hours}${minutes}${seconds}${milliseconds}`.toString()),
  };
}

export const formatTimestampToDatetime = (timestamp:any) => {
  if(!timestamp || isNaN(timestamp)) {
    return timestamp;
  }
  timestamp = Number(timestamp);
  const date = new Date(timestamp * 1000); // 秒转毫秒
  return date.toLocaleString(); // 返回本地格式的日期时间
}

export const formatSampletime = (timestamp: number) => {
  if(!timestamp) {
    return "-"
  }
  const date = new Date(Number(timestamp) / 1000); // 微秒转毫秒
  const formattedDate = `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}.${parseInt((timestamp % 1000000)).toString().padStart(6, '0')}`;
  return formattedDate
}
